<include file="Public/header" />
<title>添加账册</title>
</head>
<body>
<div class="pd-20">
	<table class="table table-border table-bordered table-bg" style="margin-bottom:20px;">
		<thead>
			<tr>
				<th scope="col" colspan="15">商品列表</th>
			</tr>
			<tr class="text-c">
				<th width=""></th>
				<th width="">条形码</th>
				<th width="">数量</th>
				<th width="">品名</th>
				<th width="">单价</th>
				<th width="">操作</th>
			</tr>
		</thead>
		<tbody>
		<foreach name="pgoods_list" item="pgv">
			<tr class="text-c tgoods">
				<td>
					<select name="goods_list" class="select input-text" style="width:150px;float:left;margin-right:5px;">
							<option value="0">---请选择---</option>
							<foreach name="goods_list" item="vo">
								<option value="<{$vo.bar_code}>" attr_price="<{$vo.goods_price}>" attr_id="<{$vo.id}>" 
								<if condition="$pgv['goods_no'] eq $vo['goods_no']">selected</if>><{$vo.goods_name}></option>
							</foreach>
					</select>
				</td>
				<td>
					<input type="text"  name="bar_code" class="input-text" value="" >
				</td>
				<td>
					<span class="de">-</span>
						<input type="text"  name="goods_nums" class="input-text" style="width:100px" value="<{$pgv['goods_nums']}>" >
					<span class="ad">+</span>
				</td>
				<td>
					<input type="text"  name="goods_name" class="input-text" value="" >
				</td>
				<td>
					<input type="text"  name="goods_price" class="input-text" value="" >
					<input type="hidden"  name="goods_id" class="input-text" value="" >
				</td>
				<td>
					<a title="添加" href="javascript:;" onclick="add_g()" class="ml-5" style="text-decoration:none">添加</a>
					<a title="删除" href="javascript:;" class="del_g ml-5" style="text-decoration:none"><i class="Hui-iconfont"></i></a>
				</td>
			</tr>
		</foreach>
		</tbody>
	</table>
<form action="__URL__/add_goods" name="postForm" id="postForm" method="post" >
	<table class="table table-border table-bordered table-bg">
		<input type="hidden" name="id" class="input-text" value="<{$detail.id}>">
		<tbody>
			<tr class="text-c">
				<td width="15%" class="text-r" style="background-color:#f5f5f5">订单类型</td>
				<td class="text-l" width="35%">
					<select name="order_cate2" class="select input-text" style="width:150px;float:left;margin-right:5px;">
						<option value="0">---请选择---</option>
						<option value="1"<if condition="$detail['order_cate2'] eq 1">selected</if> >现货</option>
						<option value="2"<if condition="$detail['order_cate2'] eq 2">selected</if> >直邮</option>
						<option value="3"<if condition="$detail['order_cate2'] eq 3">selected</if> >保税</option>
					</select>
				</td>
				<td width="15%" class="text-r" style="background-color:#f5f5f5">订单编号</td>
				<td class="text-l" width="35%">
					<input type="text" name="order_no" readonly class="input-text" value="<{$detail.order_no}>" >
				</td>
			</tr>
			<tr class="text-c">
				<td width="15%" class="text-r" style="background-color:#f5f5f5">收件人姓名</td>
				<td class="text-l" width="35%">
					<!-- <input type="text" name="user_name" class="input-text" value="<{$detail.user_name}>" > -->
					<input type="text" name="user_name" class="input-text" style="width:137px;float:left;" value="<{$detail.user_name}>" >
					<select name="idcard_id" class="select input-text" style="width:100px;float:left;margin-right:5px;">
						<option value="0">---请选择---</option>
						<foreach name="userlist" item="vo">
							<option value="<{$vo.id}>" <if condition="$detail['idcard_id'] eq $vo['id']">selected</if> ><{$vo.name}></option>
						</foreach>
					</select>
				</td>
				<td width="15%" class="text-r" style="background-color:#f5f5f5">发件人姓名</td>
				<td class="text-l">
					<!-- <input type="text" name="sendMan" class="input-text" value="<{$detail.sendman}>" > -->
					<input type="text" name="sendMan" class="input-text" style="width:137px;float:left;" value="<{$detail.sendman}>" >
					<select name="sidcard_id" class="select input-text" style="width:100px;float:left;margin-right:5px;">
						<option value="0">---请选择---</option>
						<foreach name="userlist" item="vo">
							<option value="<{$vo.id}>" <if condition="$detail['idcard_id'] eq $vo['id']">selected</if> ><{$vo.name}></option>
						</foreach>
					</select>
				</td>
			</tr>
			<tr class="text-c">
				<td width="15%" class="text-r" style="background-color:#f5f5f5">收件人身份证号</td>
				<td class="text-l">
					<input type="text" name="user_idcard" class="input-text" value="<{$detail.user_idcard}>" >
				</td>				
				<td width="15%" class="text-r" style="background-color:#f5f5f5"></td>
				<td class="text-l">
					
				</td>
			</tr>
			<tr class="text-c">				
				<td width="15%" class="text-r" style="background-color:#f5f5f5">收件人电话(手机号)</td>
				<td class="text-l" width="35%">
					<input type="text" name="user_phone" class="input-text" value="<{$detail.user_phone}>" >
				</td>
				<td width="15%" class="text-r" style="background-color:#f5f5f5">发件人电话(手机号)</td>
				<td class="text-l">
					<input type="text" name="sendManPhone" class="input-text" value="<{$detail.sendmanphone}>" >
				</td>
			</tr>
			<tr class="text-c">
				<td width="15%" class="text-r" style="background-color:#f5f5f5">收件人省</td>
				<td class="text-l">
					<!-- <input type="text" name="province" class="input-text" value="<{$detail.province}>" > -->
					<select name="province" class="select input-text" style="width:250px;float:left;margin-right:5px;">
						<option value="0">---请选择---</option>
						<foreach name="province_list" item="vo">
							<option value="<{$vo['fullName']}>"<if condition="$vo['fullName'] eq $detail['province']">selected</if> ><{$vo.fullName}></option>
						</foreach>
					</select>
				</td>
				<td width="15%" class="text-r" style="background-color:#f5f5f5">发件人省</td>
				<td class="text-l">
					<!-- <input type="text" name="sendProvince" class="input-text" value="<{$detail.sendprovince}>" > -->
					<select name="sendProvince" class="select input-text" style="width:250px;float:left;margin-right:5px;">
						<option value="0">---请选择---</option>
						<foreach name="province_list" item="vo">
							<option value="<{$vo['fullName']}>"<if condition="$vo['fullName'] eq $detail['sendprovince']">selected</if> ><{$vo.fullName}></option>
						</foreach>
					</select>
				</td>
			</tr>
			<tr class="text-c">
				<td width="15%" class="text-r" style="background-color:#f5f5f5">收件人市</td>
				<td class="text-l" width="35%">
					<select name="city" class="select input-text" style="width:250px;float:left;margin-right:5px;">
						<option value="0">---请选择---</option>
						<foreach name="city_list" item="vo">
							<option value="<{$vo['fullName']}>"<if condition="$vo['fullName'] eq $detail['city']">selected</if> ><{$vo.fullName}></option>
						</foreach>
					</select>
				</td>
				<td width="15%" class="text-r" style="background-color:#f5f5f5">发件人市</td>
				<td class="text-l">
					<select name="sendCity" class="select input-text" style="width:250px;float:left;margin-right:5px;">
						<option value="0">---请选择---</option>
						<foreach name="send_city_list" item="vs">
							<option value="<{$vs['fullName']}>"<if condition="$vs['fullName'] eq $detail['sendcity']">selected</if> ><{$vs.fullName}></option>
						</foreach>
					</select>
				</td>
			</tr>
			<tr class="text-c">
				<td width="15%" class="text-r" style="background-color:#f5f5f5">收件人区/县</td>
				<td class="text-l">
					<!-- <input type="text" name="area" class="input-text" value="<{$detail.area}>" > -->
					<select name="area" class="select input-text" style="width:250px;float:left;margin-right:5px;">
						<option value="0">---请选择---</option>
						<foreach name="area_list" item="vo">
							<option value="<{$vo['fullName']}>"<if condition="$vo['fullName'] eq $detail['area']">selected</if> ><{$vo.fullName}></option>
						</foreach>
					</select>
				</td>
				<td width="15%" class="text-r" style="background-color:#f5f5f5">发件人区/县</td>
				<td class="text-l">
					<!-- <input type="text" name="sendCounty" class="input-text" value="<{$detail.sendcounty}>" > -->
					<select name="sendCounty" class="select input-text" style="width:250px;float:left;margin-right:5px;">
						<option value="0">---请选择---</option>
						<foreach name="send_area_list" item="vo">
							<option value="<{$vo['fullName']}>"<if condition="$vo['fullName'] eq $detail['sendcounty']">selected</if> ><{$vo.fullName}></option>
						</foreach>
					</select>
				</td>
			</tr>
			<tr class="text-c">
				<td width="15%" class="text-r" style="background-color:#f5f5f5">收件人详细地址</td>
				<td class="text-l">
					<input type="text" name="user_address" class="input-text" value="<{$detail.user_address}>" >
				</td>
				<td width="15%" class="text-r" style="background-color:#f5f5f5">发件人详细地址</td>
				<td class="text-l">
					<input type="text" name="sendManAddress" class="input-text" value="<{$detail.sendmanaddress}>" >
				</td>
			</tr>
			
			<tr class="text-c">
				<td width="15%" class="text-r" style="background-color:#f5f5f5">运单号</td>
				<td class="text-l">
					<input type="text" name="logistics_no" class="input-text"  value="<{$detail.logistics_no}>" >
				</td>
				<td width="15%" class="text-r" style="background-color:#f5f5f5">快递公司</td>
				<td class="text-l">
					<select name="logistics_business" class="select input-text" style="width:150px;float:left;margin-right:5px;">
						<option value="0">---请选择---</option>
						<foreach name="ec_list" item="vo">
							<option value="<{$vo.code}>"<if condition="$detail['logistics_business'] eq $vo['code']">selected</if> ><{$vo.name}></option>
						</foreach>
					</select>
				</td>
			</tr>
			<tr class="text-c">
				<td width="15%" class="text-r" style="background-color:#f5f5f5">包裹重量</td>
				<td class="text-l">
					<input type="text" name="pack_weight" class="input-text"  value="<{$detail.pack_weight}>" >
				</td>
				<td width="15%" class="text-r" style="background-color:#f5f5f5">付款状态</td>
				<td class="text-l">
					<select name="pay_status" class="select input-text" style="width:150px;float:left;margin-right:5px;">
						<option value="0">---请选择---</option>
						<option value="1"<if condition="$detail['pay_status'] eq 1">selected</if> >已扣费</option>
						<option value="2"<if condition="$detail['pay_status'] eq 2">selected</if> >未扣费</option>
					</select>
				</td>
			</tr>
			<tr class="text-c">
				<td width="15%" class="text-r" style="background-color:#f5f5f5">总价</td>
				<td class="text-l" width="35%">
					<input type="text" name="total_price" class="input-text" value="<{$detail.total_price}>" >
				</td>
				<td width="15%" class="text-r" style="background-color:#f5f5f5">状态</td>
				<td class="text-l" width="35%">
					<img src="__PUBLIC__/img/flag/flag_green.png">
					<input type="radio" name="flag" value="1" checked="">
					<img src="__PUBLIC__/img/flag/flag_yellow.png"></img>
					<input type="radio" name="flag" value="2">
					<img src="__PUBLIC__/img/flag/flag_red.png">
					<input type="radio" name="flag" value="3">
				</td>
			</tr>
			<tr class="text-c">
				<td width="15%" class="text-r" style="background-color:#f5f5f5">备注1</td>
				<td class="text-l" width="35%">
					<textarea name="mark1" class="textarea" ><{$detail.mark1}></textarea>
				</td>
				<td width="15%" class="text-r" style="background-color:#f5f5f5">备注2</td>
				<td class="text-l" width="35%">
					<textarea name="mark2" class="textarea" ><{$detail.mark2}></textarea>
				</td>
			</tr>
		</tbody>
	</table>
	<input type="button" class="btn btn-primary radius mt" id="b_add" value="更新">
</form>
</div>
<script type="text/javascript" src="__PUBLIC__/js/jquery.form.js"></script>
<script>

	function add_g(){
			tr="<tr  class='text-c'>"+$('.tgoods').html()+"</tr>";
			$('.tgoods').parent().append(tr);
		}
		//总价
		function ttotal_price(){
			weight=$('input[name=pack_weight]').val();
			logistics_business=$('select[name=logistics_business]').find('option:selected').val();
			province=$('select[name=province]').find('option:selected').val();
			<!-- layer.msg(weight); -->
			var fee_ems=0;
			if(weight==""){
				fee_ems=0;//快递费用
			}
			if(logistics_business==0){
				fee_ems=0;//快递费用
			}
			if(province==0){
				fee_ems=0;//快递费用
			}
			$.ajax({
				type:'POST',
				data:{weight:weight,area:province,company_code:logistics_business},
				url:"<{:U('Ems/ems_fee')}>",
				success:function(data){
					if(data.status){
						<!-- console.log(data.total); -->
						<!-- return data.total; -->
						order_cate2=$('select[name=order_cate2]').find('option:selected').val();
						if(order_cate2!=2){//直邮
							fee_ems=data.total;
						}
						
						goods_nums=$('.tgoods').parent().find('input[name=goods_nums]');
						goods_price=$('.tgoods').parent().find('input[name=goods_price]');
						total_price=0;
						for(i=0;i<goods_nums.length;i++){
							nums=parseFloat(goods_nums.eq(i).val());
							price=parseFloat(goods_price.eq(i).val());
							if(nums==""){
								nums=0;
							}
							if(price==""){
								price=0;
							}
							total_price=total_price+nums*price+fee_ems;
						}
						$('input[name=total_price]').val(total_price);
						
					}else{
						layer.msg(data.message);
						return false;
					}
				}
			})
		}
$(function(){
		$(document).on("click",'.del_g',function(){
				tr=$('.tgoods').parent().find('tr');
				if(tr.length==1){
					layer.msg('不能删除了，至少添加一个商品！');
				}else{
					$(this).parent().parent('tr').remove();
				}
		})
		$(document).on("click",".de",function(){
				start_nums="<{:C('base_config.start_nums')}>";
				nums=$(this).parent().find('input');
				if(nums.val()==""){
					tnums=0;
				}else{
					tnums=nums.val();
				}
				nums.val(parseInt(tnums)-parseInt(start_nums));
				nums.change();
		})
		$(document).on("click",".ad",function(){
				start_nums="<{:C('base_config.start_nums')}>";
				nums=$(this).parent().find('input');
				if(nums.val()==""){
					tnums=0;
				}else{
					tnums=nums.val();
				}
				nums.val(parseInt(tnums)+parseInt(start_nums));
				nums.change();
		})
		$(document).on("blur","input[name=pack_weight]",function(){//包裹重量改变
					ttotal_price();
		})
		$(document).on("change","select[name=order_cate2]",function(){//订单类型改变
					ttotal_price();
		})
		$(document).on("change","select[name=province]",function(){//收件人地址改变
					ttotal_price();
		})
		$(document).on("change","select[name=logistics_business]",function(){//快递改变
					ttotal_price();
		})
		$(document).on("change","input[name=goods_nums]",function(){
					ttotal_price();
		})
		$(document).on("change","input[name=goods_price]",function(){  
					ttotal_price();
		})
		//商品列表
		$(document).on("change","select[name=goods_list]",function(){  
		<!-- $("select[name=goods_list]").on('change',function(e){ -->
				bar_code=$(this).find('option:selected').val();
				goods_price=$(this).find('option:selected').attr('attr_price');
				goods_name=$(this).find('option:selected').text();
				goods_id=$(this).find('option:selected').attr('attr_id');
				
				goods_ids=$('input[name=goods_id]');
				for(i=0;i<goods_ids.length;i++){
					if(goods_id==goods_ids.eq(i).val()){
						layer.msg('此商品已选择过了');
						return false;
					}
				}
				<!-- layer.msg(bar_code); -->
				td=$(this).parent().parent().find('td');
				td.eq(1).find('input').val(bar_code);
				td.eq(3).find('input').val(goods_name);
				td.eq(4).find('input').eq(0).val(goods_price);
				td.eq(4).find('input').eq(1).val(goods_id);

		})
		$('select[name=goods_list]').change();
		$("select[name=province]").change(function(){
				$.ajax({
					type: 'POST',
					data: {code:$(this).val()},
					url: "<{:U('city_list')}>",
					success:function(data){
						<!-- console.log(data.message.length); -->
						if(data.status==1){
							$("select[name=city]").find('option').remove();
							$("select[name=area]").find('option').remove();
							$("select[name=area]").append("<option value='0'>---请选择---</option>");
							$("select[name=city]").append("<option value='0'>---请选择---</option>");
							for(i=0;i<data.message.length;i++){
								html="<option value='"+data.message[i].fullName+"'>"+data.message[i].fullName+"</option>";
								$("select[name=city]").append(html);
							}
						}else{			
							layer.msg(data.message);
						}
						return false;
					}
				})
			})
			$("select[name=city]").change(function(){
				province_name=$("select[name=province]").find('option:checked').val();
				$.ajax({
					type: 'POST',
					data: {province_name:province_name,code:$(this).val()},
					url: "<{:U('area_list')}>",
					success:function(data){
						if(data.status==1){
							$("select[name=area]").find('option').remove();
							$("select[name=area]").append("<option value='0'>---请选择---</option>");
							for(i=0;i<data.message.length;i++){
								html="<option value='"+data.message[i].fullName+"'>"+data.message[i].fullName+"</option>";
								$("select[name=area]").append(html);
							}
						}else{			
							layer.msg(data.message);
						}
						return false;
					}
				})
			})
			$("select[name=sendProvince]").change(function(){
				$.ajax({
					type: 'POST',
					data: {code:$(this).val()},
					url: "<{:U('city_list')}>",
					success:function(data){
						<!-- console.log(data.message.length); -->
						if(data.status==1){
							$("select[name=sendCity]").find('option').remove();
							$("select[name=sendCounty]").find('option').remove();
							$("select[name=sendCounty]").append("<option value='0'>---请选择---</option>");
							$("select[name=sendCity]").append("<option value='0'>---请选择---</option>");
							for(i=0;i<data.message.length;i++){
								html="<option value='"+data.message[i].fullName+"'>"+data.message[i].fullName+"</option>";
								$("select[name=sendCity]").append(html);
							}
						}else{			
							layer.msg(data.message);
						}
						return false;
					}
				})
			})
			$("select[name=sendCity]").change(function(){
				province_name=$("select[name=sendProvince]").find('option:checked').val();
				$.ajax({
					type: 'POST',
					data: {province_name:province_name,code:$(this).val()},
					url: "<{:U('area_list')}>",
					success:function(data){
						if(data.status==1){
							$("select[name=sendCounty]").find('option').remove();
							$("select[name=sendCounty]").append("<option value='0'>---请选择---</option>");
							for(i=0;i<data.message.length;i++){
								html="<option value='"+data.message[i].fullName+"'>"+data.message[i].fullName+"</option>";
								$("select[name=sendCounty]").append(html);
							}
						}else{			
							layer.msg(data.message);
						}
						return false;
					}
				})
			})
	$("select[name=idcard_id]").change(function(){
		var user_name=$("input[name=user_name]");
		var user_idcard=$("input[name=user_idcard]");
		var user_phone=$("input[name=user_phone]");
		var province=$("select[name=province]");
		var city=$("select[name=city]");
		var area=$("select[name=area]");
		var user_address=$("input[name=user_address]");
		
		if($(this).val()!=0){
			user_name.attr('readonly','readonly');
			user_name.val($(this).find("option:selected").text());
			$.ajax({
				type: 'POST',
				data: {idcard_id:$(this).val()},
				url: "<{:U('idcard_info')}>",
				success:function(data){
					console.log(data);
					if(data.status==1){	
							var message=data.message;
							user_idcard.val(message.no);
							user_phone.val(message.tel);
							province.val(message.province);
							city.find('option').remove();
							city.append("<option value='"+message.city+"'>"+message.city_name+"</option>");
							area.find('option').remove();
							area.append("<option value='"+message.area+"'>"+message.area_name+"</option>");
							user_address.val(message.address);
					}else{			
						layer.msg(data.message);
					}
					return false;
				}
			})
		}else{
			user_name.removeAttr('readonly');
			user_name.val('');
			user_idcard.val('');
			user_phone.val('');
			province.val(0);
			city.find('option').remove();
			city.append("<option value='0'>---请选择---</option>");
			area.find('option').remove();
			area.append("<option value='0'>---请选择---</option>");
			user_address.val('');
		}
	})
	$("select[name=sidcard_id]").change(function(){		
		var sendMan=$("input[name=sendMan]");
		var sendManPhone=$("input[name=sendManPhone]");
		var sendProvince=$("select[name=sendProvince]");
		var sendCity=$("select[name=sendCity]");
		var sendCounty=$("select[name=sendCounty]");
		var sendManAddress=$("input[name=sendManAddress]");
		
		if($(this).val()!=0){
			sendMan.attr('readonly','readonly');
			sendMan.val($(this).find("option:selected").text());
			$.ajax({
				type: 'POST',
				data: {idcard_id:$(this).val()},
				url: "<{:U('idcard_info')}>",
				success:function(data){
					console.log(data);
					if(data.status==1){	
							var message=data.message;
							sendManPhone.val(message.tel);
							sendProvince.val(message.province);
							sendCity.find('option').remove();
							sendCity.append("<option value='"+message.city+"'>"+message.city_name+"</option>");
							sendCounty.find('option').remove();
							sendCounty.append("<option value='"+message.area+"'>"+message.area_name+"</option>");
							sendManAddress.val(message.address);
					}else{			
						layer.msg(data.message);
					}
					return false;
				}
			})
		}else{
			sendMan.removeAttr('readonly');
			sendMan.val('');
			sendManPhone.val('');
			sendProvince.val(0);
			sendCounty.find('option').remove();
			sendCounty.append("<option value='0'>---请选择---</option>");
			sendCity.find('option').remove();
			sendCity.append("<option value='0'>---请选择---</option>");
			sendManAddress.val('');
		}
	})
	$("#b_add").click(function() {
		goods_nums=$('.tgoods').parent().find('input[name=goods_nums]');
		goods_price=$('.tgoods').parent().find('input[name=goods_price]');
		bar_code=$('.tgoods').parent().find('input[name=bar_code]');
		goods_name=$('.tgoods').parent().find('input[name=goods_name]');
		goods_id=$('.tgoods').parent().find('input[name=goods_id]');
		
		order_id=$('input[name=id]').val();
		str="";
		for(i=0;i<goods_nums.length;i++){
			str=str+','+goods_nums.eq(i).val()+':'+goods_price.eq(i).val()+':'+bar_code.eq(i).val()+':'+goods_name.eq(i).val()+':'+goods_id.eq(i).val();
		}
		
        $("#postForm").ajaxSubmit({  
            url: "<{:U('add_order')}>",
            type : "post",  
            dataType : 'json',  
            success : function(data) {
				if(data.status==1){
					<!-- layer.msg(data.message,function(){ -->
						<!-- parent.window.location.href =parent.window.location.href; -->
						//添加商品
								$.ajax({
									type:'POST',
									data:{str:str.substring(1),order_id:order_id},
									url:"<{:U('tedit_goods')}>",
									success:function(data){
										if(!data.status){
											layer.msg(data.message);
											return false;
										}else{
											layer.msg(data.message,function(){
												parent.window.location.href =parent.window.location.href;
											});
										}
									}
								})
					<!-- }); -->
				}else{
					layer.msg(data.message);
				}
                
            },  
            error : function(data) {  
                layer.msg('出错');
            }  
        });  
    });
	
});
</script>
</body>
</html>